from starlette.requests import Request
from starlette.responses import JSONResponse

from main import app

"""
异常定义
"""


class ServerException(Exception):
    def __init__(self, name: str, message: str):
        self.name = name
        self.message = message
        super().__init__(self.message)


@app.exception_handler(ServerException)
async def server_exception_handler(request: Request, exc: ServerException):
    return JSONResponse(status_code=500, content={"message": f"发生{exc.name},具体信息 ：{exc.message}"}, )


class AuthException(Exception):
    def __init__(self, name: str, message: str):
        self.name = name
        self.message = message
        super().__init__(self.message)


@app.exception_handler(AuthException)
async def auth_exception_handler(request: Request, exc: AuthException):
    return JSONResponse(status_code=500, content={"message": f"发生{exc.name},具体信息 ：{exc.message}"}, )
